package com.mz.ozy.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.adapter.MyExtendListAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.NewMyExtendBean;
import com.mz.ozy.bean.NewMyExtendInfoBean;
import com.mz.ozy.mvp.presenter.ExtendPresenter;
import com.mz.ozy.mvp.view.ExtendContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyExtendListActivity extends BaseActivity<ExtendContract.View, ExtendPresenter> implements ExtendContract.View {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;

    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private int pageNo = 1;
    private int tag = 0;
    int type = 1;

    private List<NewMyExtendBean.PageDTO.PageItemsDTO> mBeanList = new ArrayList<>();
    private MyExtendListAdapter mAdapter;


    public static void go(Context context, int type) {
        Intent intent = new Intent(context, MyExtendListActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_extend_list);
        ButterKnife.bind(this);

        type = getIntent().getIntExtra("type",1);

        initTitle();
        tvCenterTitle.setText("分享用户");

        initView();
    }

    public void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyExtendListAdapter(mBeanList);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                NewMyExtendBean.PageDTO.PageItemsDTO pageItemsDTO = (NewMyExtendBean.PageDTO.PageItemsDTO) adapter.getData().get(position);
                MyExtendDetailActivity.go(MyExtendListActivity.this, pageItemsDTO.id, MyExtendListActivity.this.type + "");
            }
        });
        recyclerview.setAdapter(mAdapter);

        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.list(MyExtendListActivity.this, pageNo + "", type +"");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshList();
            }
        });

        llNoData.setVisibility(View.GONE);
    }

    private void refreshList() {
        if (mPresenter == null) return;
        tag = 0;
        pageNo = 1;
        mPresenter.list(MyExtendListActivity.this, pageNo + "", type +"");
    }

    @Override
    public void listSuccess(NewMyExtendBean bean) {
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }

        if (bean.page == null || bean.page.pageItems == null) {
            return;
        }

        mBeanList.addAll(bean.page.pageItems);
        mAdapter.notifyDataSetChanged();

        if (mBeanList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            smartRefreshLayout.setLoadmoreFinished(true);
        } else {
            llNoData.setVisibility(View.GONE);
        }
    }

    @Override
    public void infoSuccess(NewMyExtendInfoBean bean) {

    }
}